package com.ruoyi.experiment.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.experiment.mapper.ExperimentDesignMapper;
import com.ruoyi.experiment.domain.ExperimentDesign;
import com.ruoyi.experiment.service.IExperimentDesignService;

/**
 * 实验设计Service业务层处理
 * 
 * @author gw
 * @date 2025-01-08
 */
@Service
public class ExperimentDesignServiceImpl implements IExperimentDesignService 
{
    @Autowired
    private ExperimentDesignMapper experimentDesignMapper;

    /**
     * 查询实验设计
     * 
     * @param designId 实验设计主键
     * @return 实验设计
     */
    @Override
    public ExperimentDesign selectExperimentDesignByDesignId(Long designId)
    {
        return experimentDesignMapper.selectExperimentDesignByDesignId(designId);
    }

    /**
     * 查询实验设计列表
     * 
     * @param experimentDesign 实验设计
     * @return 实验设计
     */
    @Override
    public List<ExperimentDesign> selectExperimentDesignList(ExperimentDesign experimentDesign)
    {
        return experimentDesignMapper.selectExperimentDesignList(experimentDesign);
    }

    /**
     * 新增实验设计
     * 
     * @param experimentDesign 实验设计
     * @return 结果
     */
    @Override
    public int insertExperimentDesign(ExperimentDesign experimentDesign)
    {
        experimentDesign.setCreateTime(DateUtils.getNowDate());
        experimentDesign.setCreateBy(SecurityUtils.getUsername());
        return experimentDesignMapper.insertExperimentDesign(experimentDesign);
    }

    /**
     * 修改实验设计
     * 
     * @param experimentDesign 实验设计
     * @return 结果
     */
    @Override
    public int updateExperimentDesign(ExperimentDesign experimentDesign)
    {
        experimentDesign.setUpdateTime(DateUtils.getNowDate());
        experimentDesign.setUpdateBy(SecurityUtils.getUsername());
        return experimentDesignMapper.updateExperimentDesign(experimentDesign);
    }

    /**
     * 批量删除实验设计
     * 
     * @param designIds 需要删除的实验设计主键
     * @return 结果
     */
    @Override
    public int deleteExperimentDesignByDesignIds(Long[] designIds)
    {
        int deletedCount = 0;
        for (Long id : designIds){
            ExperimentDesign experimentDesign = experimentDesignMapper.selectExperimentDesignByDesignId(id);
            experimentDesign.setDeleteBy(SecurityUtils.getUsername());
            experimentDesign.setDeleteTime(DateUtils.getNowDate());
            experimentDesign.setDelFlag(1);
            experimentDesignMapper.updateExperimentDesign(experimentDesign);
            deletedCount++;
        }
        return deletedCount;
    }

    /**
     * 删除实验设计信息
     * 
     * @param designId 实验设计主键
     * @return 结果
     */
    @Override
    public int deleteExperimentDesignByDesignId(Long designId)
    {
        ExperimentDesign experimentDesign = experimentDesignMapper.selectExperimentDesignByDesignId(designId);
        experimentDesign.setDeleteBy(SecurityUtils.getUsername());
        experimentDesign.setDeleteTime(DateUtils.getNowDate());
        experimentDesign.setDelFlag(1);
        return experimentDesignMapper.updateExperimentDesign(experimentDesign);
    }
}
